import os, sys, pygame, time, traceback
from pygame.locals import *    

# Global list and definitions
global SPRITE_SHEET, CLOCK, SCREEN, SCREEN_WIDTH, SCREEN_HEIGHT    

CLOCK = pygame.time.Clock()
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768

# Initialize pygame environment
pygame.init()
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Galaga Zero: Origin Story')

# Sprite sheet class for use with image handling
class SpriteSheet(object):
    def __init__(self, filename):
        try:
            self.sheet = pygame.image.load(filename).convert_alpha()
        except Exception as error:
            print ('Unable to load spritesheet image:', filename)
            print (error)
            traceback.print_exc()

    def image_at(self, rectangle, colorkey = None):
        "Loads image from x,y,x+offset,y+offset"
        rect = pygame.Rect(rectangle)
        image = pygame.Surface(rect.size).convert_alpha()
        image.blit(self.sheet, (0, 0), rect)
        if colorkey is not None:
            if colorkey is -1:
                colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, pygame.RLEACCEL)
        return image

    def images_at(self, rects, colorkey = None):
        "Loads multiple images, supply a list of coordinates"
        return [self.image_at(rect, colorkey) for rect in rects]

    # Load a whole strip of images
    def load_strip(self, rect, image_count, colorkey = None):
        "Loads a strip of images and returns them as a list"
        tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3])
                for x in range(image_count)]
        return self.images_at(tups, colorkey)

SPRITE_SHEET = SpriteSheet("data/sprites.png")
